package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.model.RecruitmentMessage;
import com.ruoyi.system.domain.Recruitment;
import com.ruoyi.system.domain.RecruitmentResume;
import com.ruoyi.system.domain.Resume;
import com.ruoyi.system.service.IRecruitmentResumeService;
import com.ruoyi.system.service.IRecruitmentService;
import com.ruoyi.system.service.IResumeService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 信息管理Controller
 *
 * @author ruoyi
 * @date 2022-11-05
 */
@Api("信息管理")
@Controller
@RequestMapping("/system/recMes")
public class RecruitmentMessageController extends BaseController {

    private String prefix = "system/recMes";

    @Resource
    private IRecruitmentResumeService recruitmentResumeService;

    @Resource
    private IResumeService resumeService;

    @Resource
    private IRecruitmentService recruitmentService;

    @Resource
    private ISysUserService userService;

    @ApiOperation("跳转查询信息管理列表页")
    @RequiresPermissions("system:recMes:view")
    @GetMapping()
    public String resume()
    {
        return prefix + "/recMes";
    }

    /**
     * 查询信息管理列表
     */
    @ApiOperation("查询信息管理列表")
    @RequiresPermissions("system:recMes:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitmentResume recruitmentResume)
    {
        //应聘者向本公司该招聘岗位投递简历
        recruitmentResume.setFlag(0);
        startPage();
        List<RecruitmentResume> list = recruitmentResumeService.selectRecruitmentResumeList(recruitmentResume);
        List<RecruitmentMessage> messageList = new ArrayList<>();
        for (RecruitmentResume recRes : list) {
            RecruitmentMessage recruitmentMessage = new RecruitmentMessage();
            Resume resume = resumeService.selectResumeByResumeId(recRes.getResumeId());
            Recruitment recruitment = recruitmentService.selectRecruitmentByRecruitmentId(recRes.getRecruitmentId());
            //如果投递的是本公司，则将该条信息记录起来
            if (recruitment.getRecruitmentCompany().equals(ShiroUtils.getSysUser().getUserName())) {
                recruitmentMessage.setRecResId(recRes.getRecResId());
                recruitmentMessage.setRecruitmentId(recRes.getRecruitmentId());
                recruitmentMessage.setResumeId(recRes.getResumeId());
                recruitmentMessage.setRecruitmentName(recruitment.getRecruitmentName());
                recruitmentMessage.setResumeName(resume.getResumeName());
                messageList.add(recruitmentMessage);
            }
        }
        return getDataTable(messageList);
    }

    /**
     * 跳转详用户简历详情页
     * */
    @ApiOperation("跳转简历详情页面")
    @Log(title = "跳转简历详情页面", businessType = BusinessType.UPDATE)
    @GetMapping("/detail/{resumeId}")
    public String detail(@PathVariable("resumeId") Long resumeId, ModelMap mmap) {
        // 页面filter条件
        Long userId = userService.selectUserByUserName(resumeService.selectResumeByResumeId(resumeId).getResumeName()).getUserId();
        mmap.put("user", userService.selectUserById(userId));
        mmap.put("resume", resumeService.selectResumeByResumeId(Long.valueOf(resumeId)));
        return prefix + "/detail";
    }

    /**
     * 发送面试邀请
     * @param recResId
     * @return
     */
    @ApiOperation("发送面试邀请")
    @PostMapping("/interview")
    @ResponseBody
    public AjaxResult giveRecruitment(String recResId)
    {
        return AjaxResult.success(recruitmentResumeService.giveRecruitment(recResId));
    }
}
